package com.bening.smsapp.parser;


import com.bening.smsapp.bean.Asn1Bean;
import com.bening.smsapp.bootstrap.BootConfigParameters;
import com.bening.smsapp.constant.SmsAppConstant;
import com.bening.smsapp.dao.SmsAppDao;
import com.bening.smsapp.factory.SmsAppFactory;
import com.bening.smsapp.util.Asn1Digester;
import com.bening.smsapp.util.SmsAppTools;
import com.ibm.xml.asn1.ASN12SAX;
import com.ibm.xml.asn1.LabelSAX;
import com.ibm.xml.asn1.SAXEventDumper;
import com.ibm.xml.parser.DTD;
import com.ibm.xml.parser.Parser;

import java.io.*;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

public class Asn1Parser {
	
	private static BootConfigParameters bootParam = BootConfigParameters.getInstance();
	final static Logger logger = Logger.getLogger( Asn1Parser.class );
	private static SmsAppDao smsDao = SmsAppFactory.getDaoObject();
	
    public static void parseFileToDatabase( String folder, String filename , int sourcePathId)
    {
        Logger logger = Logger.getLogger( CdrFixedLengthAsciiParser.class );
        final String METHOD_SIGNATURE = "[parser]";
        
        // update start processing time
        try  {
        	SmsAppTools.updateFileStatus(filename, SmsAppConstant.FLAG_PARSE_RUNNING);
        }catch(SQLException s) {
        	logger.error(s);
        	SmsAppTools.InsertEventLog(SmsAppConstant.SQL_ERROR, s.getMessage(), sourcePathId);
        }

        try {
        	Asn1Bean b = runParser(folder, filename);
        	//smsDao.insertParsingResultAsn1(decodeValue(b), filename);
        	
        }catch(SQLException s) {
        	logger.error(s);
        	SmsAppTools.InsertEventLog(SmsAppConstant.SQL_ERROR, s.getMessage(), sourcePathId);
    	}catch(Exception i) {
    		logger.error(i);
    		SmsAppTools.InsertEventLog(SmsAppConstant.PARSE_ERROR, i.getMessage(), sourcePathId);
    	}

        if ( logger.isInfoEnabled() )
        {
            logger.info( METHOD_SIGNATURE + "updating process status to database." );
        }

        // update end processing time
        try  {
        	SmsAppTools.updateFileStatus(filename, SmsAppConstant.FLAG_PARSE_DONE);
        }catch(SQLException s) {
        	logger.error(s);
        	SmsAppTools.InsertEventLog(SmsAppConstant.SQL_ERROR, s.getMessage(), sourcePathId);
        }
        
    }

    private static Asn1Bean runParser(String folder, String filename) throws Exception {
    	Asn1Bean b = new Asn1Bean();
        String dtd = bootParam.getDtdName();
        String dtdName = bootParam.getRootName();
        
    	SAXEventDumper dump = new SAXEventDumper();
    	String tempdir = bootParam.getTempDir()+"/"+ filename +".TMP";
        Writer out = new FileWriter(tempdir);
        
        dump.setWriter(out);
        dump.setXMLDeclaration("1.0", null);

        try {
	    
	        com.jp.xml.asn1.ASN12SAX a2s = new com.jp.xml.asn1.ASN12SAX(dump);
	        a2s.setPartialDecoding(false);
	        InputStream in = new FileInputStream(folder + File.separator + filename);
	        a2s.setInputStream(in);
	        a2s.setEncoding("base64");
	        a2s.start();
	        
	        out.write('\n'+"</ROOT>");
	        out.flush();
	        out.close();
	        
        }catch(FileNotFoundException f) {
	    	  logger.error(f);
	    	  throw f;
	    }catch(SAXException s){
	    	  logger.error(s);
	    	  throw s;
	    }catch(IOException i) {
	    	  logger.error(i);
	    	  throw i;
	    }
	    return b;
    }
    
    private static DTD readDTD(String name) throws IOException {
        Parser psr = new Parser(name);
        Reader in = new FileReader(name);
        DTD dtd = psr.readDTDStream(in);
        return dtd;
      }
    private static Asn1Bean decodeValue(Asn1Bean b) {
    	Asn1Bean bx = b;
    	
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setMser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getMser(),32));
    	bx.setNser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getNser(),32));
    	bx.setXser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getXser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	bx.setAser(com.ibm.xml.asn1.Base64.decodeBitString(bx.getAser(),32));
    	
    	return bx;
    }
}
